package com.pearadmin.modules.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pearadmin.common.tools.SequenceUtil;
import com.pearadmin.common.tools.string.StringUtil;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.modules.sys.domain.SysUser;
import com.pearadmin.modules.sys.domain.WorkFix;
import com.pearadmin.modules.sys.domain.WorkFixDetail;
import com.pearadmin.modules.sys.domain.dto.AnodeBlockDto;
import com.pearadmin.modules.sys.domain.dto.WorkFixDto;
import com.pearadmin.modules.sys.mapper.WorkFixDetailMapper;
import com.pearadmin.modules.sys.mapper.WorkFixMapper;
import com.pearadmin.modules.sys.service.IWorkFixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 检修工作记录Service业务层处理
 *
 * @author Mr.Ling
 * @date 2021-11-26
 */
@Service
public class WorkFixServiceImpl implements IWorkFixService {
    @Autowired
    private WorkFixMapper workFixMapper;

    /**
     * 查询检修工作记录
     *
     * @param id 检修工作记录ID
     * @return 检修工作记录
     */
    @Override
    public WorkFix selectWorkFixById(String id) {
        return workFixMapper.selectWorkFixById(id);
    }

    /**
     * 查询检修工作记录列表
     *
     * @param workFix 检修工作记录
     * @return 检修工作记录
     */
    @Override
    public List<WorkFix> selectWorkFixList(WorkFix workFix) {
        return workFixMapper.selectWorkFixList(workFix);
    }

    /**
     * 查询检修工作记录
     *
     * @param workFix    检修工作记录
     * @param pageDomain
     * @return 检修工作记录 分页集合
     */
    @Override
    public PageInfo<WorkFix> selectWorkFixPage(WorkFix workFix, PageDomain pageDomain) {
        PageHelper.startPage(pageDomain.getPage(), pageDomain.getLimit());
        List<WorkFix> data = workFixMapper.selectWorkFixList(workFix);
        return new PageInfo<>(data);
    }

    /**
     * 新增检修工作记录
     *
     * @param workFix 检修工作记录
     * @return 结果
     */
    @Autowired
    private WorkFixDetailMapper detailMapper;

    @Override
    public int insertWorkFix(WorkFix workFix) {
        //如果没有content表示修理
        if (workFix.getContent() == null) {
            WorkFixDetail detail = BeanUtil.copyProperties(workFix, WorkFixDetail.class);
            detail.setId(SequenceUtil.makeStringId());
            detail.setFixId(workFix.getId());
            Map<String, Object> params = workFix.getParams();
            JSONArray fixType = JSONUtil.parseArray(params.get("fixType"));
            for (Object type : fixType) {
                String fType = type.toString();
                if (fType.equals("claw")) {
                    detail.setClawNum(1L);
                } else if (fType.equals("explosive")) {
                    detail.setExplosiveNum(1L);
                } else if (fType.equals("rod")) {
                    detail.setRodNum(1L);
                } else if (fType.equals("weld")) {
                    detail.setWeldNum(1L);
                } else if (fType.equals("weld_mill")) {
                    detail.setWeldMillNum(1L);
                } else {
                    detail.setOtherNum(1L);
                }
            }
            detailMapper.insertWorkFixDetail(detail);
            workFix.setIsScrap("0");
        } else {
            //1表示报废
            workFix.setIsScrap("1");
        }
        return workFixMapper.insertWorkFix(workFix);
    }

    /**
     * 修改检修工作记录
     *
     * @param workFix 检修工作记录
     * @return 结果
     */
    @Override
    public int updateWorkFix(WorkFix workFix) {
        return workFixMapper.updateWorkFix(workFix);
    }

    /**
     * 删除检修工作记录对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWorkFixByIds(String[] ids) {
        return workFixMapper.deleteWorkFixByIds(ids);
    }

    /**
     * 删除检修工作记录信息
     *
     * @param id 检修工作记录ID
     * @return 结果
     */
    @Override
    public int deleteWorkFixById(String id) {
        return workFixMapper.deleteWorkFixById(id);
    }

    @Override
    public int batchInsertWorkFix(WorkFixDto dto, SysUser sysUser) throws Exception {
        int result = 0;
        if (!CollectionUtil.isEmpty(dto.getList())) {
            List<AnodeBlockDto> list = dto.getList();
            for (AnodeBlockDto abDto : list) {
                if (StringUtil.isEmpty(abDto.getAnodeCode())) {
                    throw new Exception("编号不能为空");
                }
                if (StringUtil.isEmpty(abDto.getBlockType())) {
                    throw new Exception("导杆类型不能为空");
                }
                WorkFix workFix = BeanUtil.copyProperties(dto, WorkFix.class);
                workFix.setParams(dto.getParams());
                workFix.setAnodeCode(abDto.getAnodeCode());
                workFix.setBlockType(abDto.getBlockType());
                workFix.setCreateTime(LocalDateTime.now());
                workFix.setCreateBy(sysUser.getUserId());
                workFix.setCreateName(sysUser.getRealName());
                workFix.setId(SequenceUtil.makeStringId());
                result = insertWorkFix(workFix);
            }
        }

        return result;
    }
}
